package cn.com.server.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.alibaba.fastjson.JSON;

import cn.com.server.common.util.SigarUtil;
import cn.com.server.entity.Info;
import cn.com.server.service.InfoService;
import cn.com.server.task.entity.InfoJson;
import cn.com.server.task.entity.InfoJsonView;

@DisallowConcurrentExecution
public class QuartzJob implements Job {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			Map<String,Object> map = SigarUtil.getFileSystem();
			InfoService infoService = (cn.com.server.service.InfoService) context.getMergedJobDataMap().get("InfoService");
			InfoJsonView infoJsonView = new InfoJsonView();
			InfoJson cpu = new InfoJson();
			InfoJson mem = new InfoJson();
			InfoJson ssd = new InfoJson();
			cpu.setAvailable((String)map.get("cpuavailable"));
			cpu.setUsed((String)map.get("cpuused"));
			cpu.setTotal((String)map.get("cputotal"));
			cpu.setUsedPercent((double)map.get("cpu"));
			
			mem.setAvailable((String)map.get("memAvailu"));
			mem.setUsed((String)map.get("memUsedused"));
			mem.setTotal((String)map.get("memUsedtotal"));
			mem.setUsedPercent((double)map.get("mem"));
			
			ssd.setAvailable((String)map.get("sdavailable"));
			ssd.setUsed((String)map.get("sdused"));
			ssd.setTotal((String)map.get("sdtotal"));
			ssd.setUsedPercent((double)map.get("sd"));
			
			infoJsonView.setCpu(cpu);
			infoJsonView.setMem(mem);
			infoJsonView.setSsd(ssd);
			infoJsonView.setCreateTime(sdf.format(new Date()));
			
			Info entity = new Info();
			entity.setName("sysoInfo");
			entity.setType(0);
			entity.setSysInfos(JSON.toJSONString(infoJsonView));
			infoService.insertAndDelete(entity);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}
